import Cache from '@antmjs/cache'
import { authorize } from '@tarojs/taro-h5'
import { AuthInfo } from '@/services'

// 和UI无关的全局数据存储在这里，和UI相关的全局数据存储在store.ts文件中

const {
  cacheGetSync,
  cacheGet,
  cacheSetSync,
  cacheSet,
  cacheRemoveSync,
  cacheRemove,
} = Cache({
  ram: {},
  loc: {
    sysInfo: undefined,
    menuButton: undefined,
    token: '',
    userId: undefined,
    authInfo: undefined,
    location: undefined,
  },
})

const cacheSetAuthInfoSync = (authInfo: AuthInfo) => {
  cacheSetSync('authInfo', authInfo)
  cacheSetSync('userId', authInfo.principal.userId as any)
  cacheSetSync('token', authInfo.token)
}

const cacheRemoveAuthInfoSync = () => {
  cacheRemoveSync('authInfo')
  cacheRemoveSync('userId')
  cacheRemoveSync('token')
}

export {
  cacheGetSync,
  cacheGet,
  cacheSetSync,
  cacheSet,
  cacheRemoveSync,
  cacheRemove,
  cacheSetAuthInfoSync,
  cacheRemoveAuthInfoSync,
}
